<?php

/**
 * Controller_discipline
 * 学科组
 */

class Controller_discipline extends MyApp {

	public function __construct()
	{

		parent::__construct();

		$this->model->staff    		= YOU::loadModel('staff');
		$this->model->appoint    	= YOU::loadModel('appoint');

	}
    /** 首页 **/
    public function actionIndex()
    {

         $this->tpl->display();
    }
	/** 学科组列表 **/
    public function actionDisciplineList()
    {	
    	// 所有老师
		$staff_lists = $this->model->staff->getAll();
		// 分页
		YOU::loadLib('page', "", '0');
		$tmp = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_discipline`");
		$count = $tmp['count'];
		$page_total = ceil($count/PAGE_LIMIT);
		// 当前页
		$p = intval($_GET['p']);
		if ($p == 0){
			$p  = 1;
		}
		if ($p > 1){
			$start = PAGE_LIMIT * ($p-1);
		}else{
			$start = 0;
		}
		
		$discipline_lists = $this->db->fetchAll("SELECT * FROM `tbl_discipline` ORDER BY discipline_id DESC LIMIT {$start}, ".PAGE_LIMIT."");
		foreach ($discipline_lists as $key => $value){
			// 普通员工
			$row  = $this->model->appoint->getDisc($value['discipline_id']);
			foreach ($row as $k => $v){
				// 员工姓名
				$rows  = $this->model->staff->getinfo($v['staff_id']);
				$discipline_lists[$key]['staff_name'][$v['staff_id']] = $rows['staff_name'];
			}
		}
		// 添加
		if ($_POST) {
			$discipline_id     		= $this->post->discipline_id;
			$staff_id     			= $this->post->staff_id;

			foreach ($staff_id as $key => $value){

				// 查找任职表
				$appoint_data =$this->model->appoint->getDiscRow($value,$discipline_id);
				if (!empty($appoint_data)) {

					// 写入数据库
					$info = array(

					"discipline_id"      	=> $discipline_id,
					"staff_id"      		=> $value,
					"appoint_common"      	=> 1,
					"create_time"           => date("Y-m-d H:i:s"),

					);
					$this->db->do_update("tbl_appoint", $info, "appoint_id='{$appoint_id}'");
				}else {

					// 写入数据库
					$info = array(

					"discipline_id"      	=> $discipline_id,
					"staff_id"      		=> $value,
					"appoint_common"      	=> 1,
					"status"                => 1,
					"create_time"           => date("Y-m-d H:i:s"),
					"update_time"           => "0000-00-00 00:00:00",

					);

					$this->db->do_insert("tbl_appoint", $info);
				}
			}
			Helper_Client::location("添加成员成功", "index.php?a=discipline.disciplinelist");
		}
		
		$page = new Page(array('total' => $count, 'perpage' => PAGE_LIMIT));
		$page->next_page   = '下一页>';
		$page->pre_page    = '<上一页';
		$page->style_path  = "static/page_style";
		$this->tpl->assign("page", $page->show(4, "yahoo"));
		
        $this->tpl->assign("discipline_lists", $discipline_lists);
        $this->tpl->assign("staff_lists", $staff_lists);
        $this->tpl->display('discipline/discipline.list.tpl.php');     
    }
    /** 学科组添加 **/
    public function actionDisciplineAdd()
    {        
       if ($_POST){ 
            
            $discipline_name     	= $this->post->discipline_name;         
		
            // 写入数据库
            $info = array(       
			
            "discipline_name"      	=> $discipline_name,                        
            "status"                => 1,
            "create_time"           => date("Y-m-d H:i:s"),
            "update_time"           => "0000-00-00 00:00:00",  
			
            );
            $this->db->do_insert("tbl_discipline", $info);

            // 跳转
            Helper_Client::location("添加成功", "index.php?a=discipline.disciplinelist");
        }
        
      
        $this->tpl->display('discipline/discipline.add.tpl.php');
    }
    /** 学科组修改 **/
    public function actionDisciplineUpdate()
    {
	    $discipline_id   = (int)$this->get->discipline_id;
        $discipline_data = $this->db->fetchROW("SELECT * FROM `tbl_discipline` WHERE `discipline_id`='{$discipline_id}'");
	
        if (empty($discipline_data)){
            Helper_Client::alert("年级不存在");
        }

        if ($_POST){
            
            $discipline_name     	    = $this->post->discipline_name;

            // 写入数据库
            $info = array(  
    
            "discipline_name"      	=> $discipline_name,                   
            "status"                => 1,
            "create_time"           => date("Y-m-d H:i:s"),
            "update_time"           => "0000-00-00 00:00:00",           
            );
            $this->db->do_update("tbl_discipline", $info, "discipline_id='{$discipline_id}'");
      
            // 跳转
            Helper_Client::location("	修改成功", "index.php?a=discipline.disciplineList");
        }
    
        $this->tpl->assign("discipline_data", $discipline_data);
        $this->tpl->display('discipline/discipline.update.tpl.php');
    }
    /** 学科组删除 **/
    public function actionDisciplineDel()
    {
        $discipline_id = $this->get->discipline_id;   
        $this->db->query("DELETE  FROM `tbl_discipline` WHERE discipline_id='{$discipline_id}'");

        // 跳转
        Helper_Client::location("删除成功", "index.php?a=discipline.disciplineList");
    }
     /** 学科组批量生成 **/
    public function actiondisciplinebatch()
    {      	
        $grade_list      = $this->db->fetchAll("SELECT `grade_name`,`grade_id` FROM `tbl_grade`");
        $subject_list    = $this->db->fetchAll("SELECT `subject_name`,`subject_id` FROM `tbl_subject`");		
	
        if ($_POST){ 
         
         $grade_id     	  = $this->post->grade_id;
       	 $subject_id      = $this->post->subject_id;
       	 $discipline_name   = $this->post->discipline_name;
		 $grade_date      = $this->db->fetchROW("SELECT `grade_name` FROM `tbl_grade` WHERE grade_id='{$grade_id}'");
         $subject_date    = $this->db->fetchROW("SELECT `subject_name` FROM `tbl_subject` WHERE subject_id='{$subject_id}'");
		 $subject_name    = $subject_date['subject_name'];
		 $grade_name      = $grade_date['grade_name'];
		// 批量添加
		 if($grade_id == 0){
		 	foreach ($grade_list as $k => $l){
		 	$grade_name= $l['grade_name'];
		 	$info = array(       
			
            "discipline_name"      	=> $grade_name.$subject_name.'学科组',                                           
            "status"                => 1,
            "create_time"           => date("Y-m-d H:i:s"),
            "update_time"           => "0000-00-00 00:00:00",  
			
            );
            $this->db->do_insert("tbl_discipline", $info);
		 	}
		 	
		 }else{
            //  单条添加
            $info = array(       
			
            "discipline_name"      	=> $grade_name.$subject_name.'学科组',
                                              
            "status"                => 1,
            "create_time"           => date("Y-m-d H:i:s"),
            "update_time"           => "0000-00-00 00:00:00",  
			
            );
            
            $this->db->do_insert("tbl_discipline", $info);
		 }
            // 跳转
            Helper_Client::location("批量添加成功", "index.php?a=discipline.disciplinelist");
        }
        $this->tpl->assign("grade_list", $grade_list);  
        $this->tpl->assign("subject_list", $subject_list);  
        $this->tpl->display('discipline/discipline.batch.tpl.php');
    }
   
}
	
?>